const DB = require('../tools/mysql')

module.exports = async ctx => {

  const { version } = ctx.request.body

  const [
    [data]
  ] = await DB.raw(
    `
    SELECT
      version.detail AS detail,
      version.version AS version,
      files.name AS apkname,
      DATE_FORMAT(version.time, "%Y-%c-%d %H:%i:%s") AS time,
      files.src AS src
    FROM
      version
    LEFT JOIN files ON version.apk = files.id
    WHERE
      version.time = (
        SELECT
          max(time)
        FROM
          version
      )
    `
  )

  if (!data) {
    ctx.body = { code: 0, msg: '没发现版本信息' }
    return
  }

  if (version.replace(/\./g, '') < data.version.replace(/\./g, '')) {
    ctx.body = { code: 1, msg: '查询成功', data }
  } else {
    ctx.body = { code: 0, msg: '已经是最新版本' }
  }

}